<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row >
        <el-col :span="12">
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
    </el-card>
    <!-- 表格 -->
    <el-table border :data="roleslist" stripe style="width: 100%">
      <!-- 展开列 -->
      <el-table-column type="expand" >
        <template slot-scope="scope">
          <el-row :class="['bdbottom','felx',index === 0 ? 'bdtop' : '']" v-for="(item,index) in scope.row.children" :key="item.id">
            <!-- 渲染一级权限 -->
            <el-col :span="5" >
              <el-tag>{{item.authName}}</el-tag>
              <i class="el-icon-caret-right"></i>
            </el-col>
            <!-- 渲染二级三级权限 -->
            <el-col :span="19">
              <el-row :class="[ index2 === 0? '' : 'bdtop' ,'felx']" v-for="(item2,index2) in item.children " :key="item2.id">
                <el-col :span="8" >
                  <el-tag type="success">{{item2.authName}}</el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <el-col :span="16" >
                  <el-tag type="warning" v-for="item3 in item2.children " :key="item3.id">{{ item3.authName }}</el-tag>
                </el-col>
              </el-row>
            </el-col>

          </el-row>
          <pre></pre>
        </template>
      </el-table-column>
      <!-- 索引列 -->
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="roleName" label="角色名称" ></el-table-column>
      <el-table-column prop="roleDesc" label="角色描述" ></el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
          <el-button  size="mini" type="danger" icon="el-icon-delete">删除</el-button>
          <el-button size="mini" type="warning" icon="el-icon-setting" @click="showsetright(scope.row)">分配角色</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分配权限的对话框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="setrightVisible"
      width="50%" @close="setrightClosed()">
      <el-tree :data="rightlist" :props="treeProps" show-checkbox node-key="id" default-expand-all :default-checked-keys="checkedKeys" ref="treeRef"></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setrightVisible = false">取 消</el-button>
        <el-button type="primary" @click="allrights">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { getroleslist, getrightlist, setrights } from '@/api/index.js'

export default {
  data () {
    return {
      roleslist: [],
      setrightVisible: false,
      rightlist: [],
      // 树形对象的属性邦定
      treeProps: {
        children: 'children', // 树形对象的属性邦定
        label: 'authName'
      },
      checkedKeys: [],
      roleId: ''
    }
  },
  methods: {
    async getroleslist () {
      const res = await getroleslist()
      console.log(res, 'roleslist');

      try {
        this.roleslist = res.data
        console.log(this.roleslist, '213213213213');
      } catch (error) {
        console.log(error)
      }
    },
    async showsetright (role) {
      console.log('row', role);

      this.roleId = role.id
      // 获取所有权限的地址
      const res = await getrightlist()
      console.log(res, 'rightlist');
      // const key = res.data.children.children.id
      // console.log(key, 'key');

      try {
        this.rightlist = res.data
        // console.log(this.rightlist, 'rightlist');
        this.checkedKeys = this.getRolePermissionIds(role);
        console.log(this.checkedKeys, 'checkedKeys');

        // console.log(this.checkedKeys, 'checkedKeys');
      } catch (error) {
        console.log(error)
      }

      this.setrightVisible = true
    },
    getRolePermissionIds (role) {
      let ids = [];
      if (role.children) {
        ids = ids.concat(this.getChildrenIds(role.children));
      }
      return ids;
    },
    getChildrenIds (data) {
      let ids = [];
      data.forEach(item => {
        if (item.children) {
          ids = ids.concat(this.getChildrenIds(item.children));
        }
        if (item.id) {
          ids.push(item.id);
        }
      });
      return ids;
    },
    setrightClosed () {
      this.checkedKeys = []
      console.log(this.checkedKeys, '2131313213213213213');
    },
    async allrights () {
      const keys = [...this.$refs.treeRef.getCheckedKeys(), ...this.$refs.treeRef.getHalfCheckedKeys()]
      const id = keys.join(',')
      const res = await setrights(this.roleId, id)
      console.log(res, '1');

      if (res.meta.status !== 200) {
        return this.$message.error('分配权限失败')
      }
      this.getroleslist()
      this.setrightVisible = false
      // this.$message.error('分配权限失败')

      // 完整接口地址是什么
    }

  },

  created () {
    this.getroleslist()
  }

}
</script>

<style>
.el-tag{
  margin: 10px;
}
.bdtop{
  border-top: 1px solid #eee;
}
.bdbottom{
  border-bottom: 1px solid #eee;
}
.felx{
  display: flex;
  align-items: center;
}

</style>
